//
//  ManageBankCardVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/6.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "ManageBankCardVC.h"
#import "ImportTextView.h"
#import "MyRequestTools.h"

@interface ManageBankCardVC ()


@property (nonatomic, strong) ImportTextView *singleQuotaView;
@property (nonatomic, strong) ImportTextView *oneDayQuotaView;

@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation ManageBankCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 40, 44);
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBarBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"解绑" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}

- (void)initUI {
    
    self.title = @"银行卡管理";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *topBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenWidthMao/375*190)];
    topBackView.backgroundColor = ListMaoBackgroundColor;
    [self.view addSubview:topBackView];
    
    [self.bankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(topBackView.mas_left).offset(10);
        make.top.mas_equalTo(topBackView.mas_top).offset(15);
        make.right.mas_equalTo(topBackView.mas_right).offset(-10);
        make.bottom.mas_equalTo(topBackView.mas_bottom).offset(-15);
    }];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.left.mas_equalTo(self.bankImageView.mas_left).offset(15);
        make.top.mas_equalTo(self.bankImageView.mas_top).offset(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoImageView.mas_right).offset(10);
        make.centerY.mas_equalTo(self.logoImageView);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bankImageView.mas_left).offset(20);
        make.bottom.mas_equalTo(self.bankImageView.mas_bottom).offset(-40);
    }];
    
    [self.singleQuotaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(topBackView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.oneDayQuotaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.singleQuotaView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(self.oneDayQuotaView.mas_bottom).offset(20);
        make.height.mas_equalTo(44);
    }];
    
    [self.bankImageView sd_setImageWithURL:[NSURL URLWithString:self.model.background] placeholderImage:[UIImage imageNamed:@"card_replace"]];
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:self.model.logo] placeholderImage:[UIImage imageNamed:@"logo_replace"]];
    self.titleLabel.text = self.model.bankName;
    self.numberLabel.text = self.model.bankCardnumber;
    CGFloat limit = [self.model.limit floatValue];
    self.singleQuotaView.leftTextView.text = [NSString stringWithFormat:@"￥%.2f",limit];
    self.oneDayQuotaView.leftTextView.text = [NSString stringWithFormat:@"￥%.2f",[self.model.dayLimit floatValue]];
    
    if ([self.model.inUse isEqualToString:@"1"]) {
         self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]init]];
    }
    if ([self.model.isDefault isEqualToString:@"1"]) {
        self.nextBtn.hidden = YES;
    }else{
        self.nextBtn.hidden = NO;
    }
}





- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)rightBarBtnAction {
    
    NSString *str = self.model.bankCardnumber;
    if (str.length >3) {
        str = [str substringFromIndex:str.length-4];
    }
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"解绑银行卡" message:[NSString stringWithFormat:@"确定要解绑尾号是%@的银行卡？",str] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [MyRequestTools unbundleBankWithbankId:self.model.bankId Success:^(id  _Nullable responseObject) {
            if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] isEqualToString:@"200"]) {
                [HUD_Mao showSuccessMessage:@"解绑成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            
        }];
        
        
    }];
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:alertAction];
    [alertC addAction:alertCancel];
    [self presentViewController:alertC animated:YES completion:nil];
    
    
    NSLog(@"解绑");
}
- (void)tiJiaoAction {
    NSString *str = self.model.bankCardnumber;
    if (str.length >3) {
        str = [str substringFromIndex:str.length-4];
    }
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"设置默认银行卡" message:[NSString stringWithFormat:@"确定要设置尾号是%@的银行卡为默认卡？",str] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [MyRequestTools setUpBankIsDefaultWithbankId:self.model.bankId Success:^(id  _Nullable responseObject) {
            if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] isEqualToString:@"200"]) {
                [HUD_Mao showSuccessMessage:@"设置成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            
        }];
        
        
    }];
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:alertAction];
    [alertC addAction:alertCancel];
    [self presentViewController:alertC animated:YES completion:nil];
    
    
    NSLog(@"设为默认");
}


- (UIImageView *)bankImageView {
    
    if (!_bankImageView) {
        _bankImageView = [[UIImageView alloc] init];
//        [_bankImageView setContentMode:UIViewContentModeScaleAspectFit];
        _bankImageView.layer.cornerRadius = 4;
        _bankImageView.layer.masksToBounds = YES;
        
        [self.view addSubview:_bankImageView];
    }
    return _bankImageView;
}
- (UIImageView *)logoImageView {
    
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
//        [_logoImageView setContentMode:UIViewContentModeScaleAspectFit];
        _logoImageView.layer.cornerRadius = 20;
        _logoImageView.layer.masksToBounds = YES;
        
        [self.view addSubview:_logoImageView];
    }
    return _logoImageView;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
        [_titleLabel setTextAlignment:NSTextAlignmentLeft];
        
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UILabel *)numberLabel {
    
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.textColor = [UIColor whiteColor];
        _numberLabel.font = [UIFont boldSystemFontOfSize:20];
        [_numberLabel setTextAlignment:NSTextAlignmentCenter];
        
        [self.view addSubview:_numberLabel];
    }
    return _numberLabel;
}
- (ImportTextView *)singleQuotaView {
    
    if (!_singleQuotaView) {
        _singleQuotaView = [[ImportTextView alloc] init];
        _singleQuotaView.leftTextView.text = @"¥2,000.00";
        _singleQuotaView.titleLabel.text = @"单笔限额";
        _singleQuotaView.userInteractionEnabled = NO;
        _singleQuotaView.titleLabel.textColor = HexColor(0x999999);
        
        [self.view addSubview:_singleQuotaView];
    }
    return _singleQuotaView;
}
- (ImportTextView *)oneDayQuotaView {
    
    if (!_oneDayQuotaView) {
        _oneDayQuotaView = [[ImportTextView alloc] init];
        _oneDayQuotaView.leftTextView.text = @"¥2,000.00";
        _oneDayQuotaView.titleLabel.text = @"单日限额";
        _oneDayQuotaView.lineImageView.hidden = YES;
        _oneDayQuotaView.userInteractionEnabled = NO;
        _oneDayQuotaView.titleLabel.textColor = HexColor(0x999999);

        [self.view addSubview:_oneDayQuotaView];
    }
    return _oneDayQuotaView;
}
- (UIButton *)nextBtn {
    
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)HexColor(0xD6C096).CGColor, (__bridge id)HexColor(0xBEA471).CGColor];
        gradientLayer.locations = @[@0, @1];
        gradientLayer.startPoint = CGPointMake(0, 1);
        gradientLayer.endPoint = CGPointMake(1, 1);
        gradientLayer.frame = CGRectMake(0, 0, ScreenWidthMao - 40, 44);
        gradientLayer.cornerRadius = 4;
        gradientLayer.masksToBounds = YES;
        [_nextBtn.layer addSublayer:gradientLayer];
        [_nextBtn addTarget:self action:@selector(tiJiaoAction) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor whiteColor];
        label.text = @"设为默认";
        [_nextBtn addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(_nextBtn);
        }];
        
        [self.view addSubview:_nextBtn];
    }
    return _nextBtn;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
